import 'dart:async';

import 'package:get/get.dart';

import 'chart_state.dart';

class ChartLogic extends GetxController {
  final ChartState state = ChartState();
  late Timer timer;

  void scrollToBottom() {
    timer = Timer(const Duration(milliseconds: 100), () => state.scrollController.value.jumpTo(state.scrollController.value.position.maxScrollExtent));
  }

  void scrollToTop() {
    timer = Timer(const Duration(milliseconds: 100), () => state.scrollController.value.jumpTo(state.scrollController.value.position.minScrollExtent));
  }

  @override
  void onInit() {
    super.onInit();
    state.focusNode.value.addListener(() {
      state.isFocus.value = state.focusNode.value.hasFocus;
      if (state.focusNode.value.hasFocus) {
        scrollToBottom();
      }
    });

    state.textEditingController.value.addListener(() {
       state.isInput.value = state.textEditingController.value.text.isNotEmpty;
    });
  }

  @override
  void onClose() {
    state.scrollController.value.dispose();
    state.textEditingController.value.dispose();
    super.onClose();
  }
}
